package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.ClazzQueryDto;
import com.itheima.pojo.Clazz;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @GetMapping
    public R pageQuery(ClazzQueryDto dto) {
        log.info("分页条件查询班级:{}", dto);
        PageBean pageBean = clazzService.pageQuery(dto);
        return R.ok(pageBean);
    }

    @DeleteMapping("/{id}")
    @Log
    public R delete(@PathVariable Integer id) {
        log.info("根据ID删除班级：{}", id);
        clazzService.delete(id);
        return R.ok();
    }

    @PostMapping
    @Log
    public R insert(@RequestBody Clazz clazz) {
        log.info("新增班级:{}", clazz);
        clazzService.insert(clazz);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        log.info("根据ID查询班级:{}", id);
        Clazz clazz = clazzService.findById(id);
        return R.ok(clazz);
    }

    @PutMapping
    @Log
    public R update(@RequestBody Clazz clazz) {
        log.info("修改班级:{}", clazz);
        clazzService.update(clazz);
        return R.ok();
    }
    @GetMapping("/list")
    public R findAll() {
        log.info("查询所有班级");
        return R.ok(clazzService.findAll());
    }
}
